/*
 * @Description: 
 * @Author: paddies
 * @Date: 2024-06-05 23:43:10
 * @LastEditors: paddies
 * @LastEditTime: 2024-06-30 01:24:13
 * @FilePath: /shared_components/lib/luntai-components/search-panel/searchPanel.dart
 */
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:get/get_rx/src/rx_typedefs/rx_typedefs.dart';
import 'package:shared_components/build-image/buildImage.dart';
import 'package:shared_components/luntai-components/search-panel/controller.dart';
import 'package:shared_components/utils/index.dart';

class SearchPanel extends StatefulWidget {
  final String type;
  final int widthFactor;
  final Function searchAPICallback;
  final Function buildCard;
  const SearchPanel(
      {super.key,
      this.type = "",
      this.widthFactor = 1,
      required this.searchAPICallback,
      required this.buildCard});

  @override
  State<SearchPanel> createState() => _SearchPanelState();
}

GlobalKey<_SearchPanelState> globalSearchPanelKey = GlobalKey();

class _SearchPanelState extends State<SearchPanel> {
  final SearchPanelController controller = Get.put(SearchPanelController());
  ScrollController scrollController = ScrollController();
  Future<void> throwFun({String funType = ''}) async {
    if (funType == "onRefresh") {
      controller.onRefresh(searchAPICallback: widget.searchAPICallback);
    }
  }

  @override
  void initState() {
    // 添加列表滑动监听
    scrollController.addListener(() {
      // 滑动距离
      var dis = scrollController.position.maxScrollExtent -
          scrollController.position.pixels;
      // 当滑动到底部不足 300 并且没有加载数据时，加载更多
      // 也可以根据临界值，是否允许下拉刷新以及下拉加载更多
      if (dis == 0) {
        JhCommonUtils.debounce(() {
          controller.nextPage(searchAPICallback: widget.searchAPICallback);
        }, 100);
        // _loadData(loadMore: true);
      }
    });
    // TODO: implement initState
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    var screenTopHeight = MediaQuery.of(context).padding.top;
    var screenBottomHeight = MediaQuery.of(context).padding.bottom;
    return Container(
        width: double.infinity,
        height: double.infinity,
        child: Column(
          children: [
            Container(
              width: double.infinity,
              height: screenTopHeight,
              // color: Colors.red,
            ),
            Container(
              width: double.infinity,
              height: 50,
              // color: Colors.green,
              child: Row(
                children: [
                  GestureDetector(
                    onTap: () {
                      Get.back();
                    },
                    child: Container(
                      width: 80,
                      height: double.infinity,
                      // color: Colors.red,
                      alignment: Alignment.center,
                      child: Icon(
                        Icons.chevron_left_rounded,
                        size: 30,
                      ),
                    ),
                  ),
                  Expanded(
                      child: Container(
                    width: double.infinity,
                    height: double.infinity,
                    padding: EdgeInsets.only(top: 5, bottom: 5),
                    // color: Colors.teal,
                    child: Container(
                        width: double.infinity,
                        height: 50,
                        decoration: new BoxDecoration(
                          border: new Border.all(
                              width: 1.0, color: Colors.white), //边框
                          // color: Colors.grey,
                          borderRadius: new BorderRadius.all(
                              new Radius.circular(20.0)), //圆角
                        ),

                        // color: ThemeColor.myTheme,
                        child: Padding(
                            padding: const EdgeInsets.only(
                                top: 5, bottom: 5, left: 0, right: 0),
                            child: Stack(
                              alignment: Alignment.topRight,
                              children: [
                                TextFormField(
                                  enabled: true,
                                  onChanged: (value) {
                                    controller.state.searchKey.value = value;
                                  },
                                  style: const TextStyle(
                                      textBaseline: TextBaseline.alphabetic),
                                  decoration: InputDecoration(
                                    hintText: "搜索内容",
                                    // prefixIcon: const Icon(
                                    //   Icons.search,
                                    //   color: Color(0xffDFAD5C),
                                    // ),
                                    filled: true,
                                    fillColor: Colors.white,
                                    contentPadding: const EdgeInsets.only(
                                        top: 0, left: 10, bottom: 0, right: 0),
                                    border: OutlineInputBorder(
                                      borderRadius: BorderRadius.circular(20.0),
                                      borderSide: BorderSide.none,
                                    ),
                                  ),
                                ),
                              ],
                            ))),
                    // child: ,
                  )),
                  GestureDetector(
                      onTap: () {
                        print("搜索内容");
                        controller.onRefresh(
                            searchAPICallback: widget.searchAPICallback);
                        // print(controller.state.searchName.value);
                        // controller.resetHandleSearch();
                      },
                      child: Container(
                        width: 80,
                        height: double.infinity,
                        // color: Colors.red,
                        alignment: Alignment.center,
                        child: Text(
                          "搜索",
                          style: TextStyle(fontSize: 16),
                        ),
                      ))
                ],
              ),
            ),
            Expanded(
                child: Container(
              width: double.infinity,
              height: double.infinity,
              child: RefreshIndicator(
                  onRefresh: () async {
                    controller.onRefresh(
                        searchAPICallback: widget.searchAPICallback);
                  },
                  child: SizedBox(
                      width: double.infinity,
                      height: double.infinity,
                      child: SingleChildScrollView(
                        physics: const AlwaysScrollableScrollPhysics(),
                        controller: scrollController,
                        child: Obx(() => Column(children: [
                              Column(
                                children: [
                                  Visibility(
                                    visible: controller.state.list.isEmpty,
                                    child: Padding(
                                        padding:
                                            const EdgeInsets.only(top: 100),
                                        child: SizedBox(
                                            width: 180,
                                            height: 180,
                                            child: buildImage(
                                                url:
                                                    "assets/images/common/empty.png",
                                                fit: BoxFit.scaleDown))),
                                  ),
                                  Wrap(
                                      spacing: 0, //主轴上子控件的间距
                                      runSpacing: 0,
                                      children: [
                                        ...controller.state.list.value.map((e) {
                                          // return buildCard(e);
                                          return FractionallySizedBox(
                                              widthFactor:
                                                  1 / widget.widthFactor,
                                              child: widget.buildCard(e));
                                        }).toList(),
                                      ])
                                ],
                              ),
                              SizedBox(
                                height: screenBottomHeight,
                              )
                            ])),
                        //         .asMap()
                        //         .keys
                        //         .map((index) {
                        //   return Text("data");
                        // }))),
                      ))),
            ))
          ],
        ));
  }
}
